<html>
<head>
<title>GW-BASIC User's Guide</title>
<link rel="stylesheet" href="stylesheet.css">
</head>

<body>

<h1>DEF FN Statement</h1>
<h4>Purpose:</h4>
<p>To define and name a function written by the user.</p>
<h4>Syntax:</h4>
<pre><b>DEF FN</b><i>name</i>[<i>arguments</i>] <i>expression</i></pre>
<h4>Comments:</h4>
<p><span class="code"><i>name</i></span><i> </i>must be a legal variable name. This name, preceded by <span class="code">FN</span>, becomes the name of the function.</p>
<p><span class="code"><i>arguments</i></span><i> </i>consists of those variable names in the function definition that are to be replaced when the function is called. The items in the list are separated by commas.</p>
<p><span class="code"><i>expression</i></span><i> </i>is an expression that performs the operation of the function. It is limited to one statement.</p>
<p>In the <span class="code">DEF FN</span> statement, arguments serve only to define the function; they do not affect program variables that have the same name. A variable name used in a function definition may or may not appear in the argument. If it does, the value of the parameter is supplied when the function is called. Otherwise, the current value of the variable is used.</p>
<p>The variables in the argument represent, on a one-to-one basis, the argument variables or values that are to be given in the function call.</p>
<p>User-defined functions may be numeric or string. If a type is specified in the function name, the value of the expression is forced to that type before it is returned to the calling statement. If a type is specified in the function name and the argument type does not match, a &quot;<span class="code">Type Mismatch</span>&quot; error occurs.</p>
<p>A user-defined function may be defined more than once in a program by repeating the <span class="code">DEF FN</span> statement.</p>
<p>A <span class="code">DEF FN</span> statement must be executed before the function it defines may be called. If a function is called before it has been defined, an &quot;<span class="code">Undefined User Function</span>&quot; error occurs.</p>
<p><span class="code">DEF FN</span> is illegal in the direct mode.</p>
<p>Recursive functions are not supported in the <span class="code">DEF FN</span> statement.</p>
<h4>Examples:</h4>
<pre>.
.
.
400 R=1: S=2
410 DEF FNAB(X, Y)=X^3/Y^2
420 T=FNAB(R, S)
.
.
.</pre>
<p>Line 410 defines the user-defined function FNAB. The function is called in line 420. When executed, the variable T will contain the value R<sup>3</sup> divided by S<sup>2</sup>, or .25.</p>
</body>
</html>
